//
//  ViewController.m
//  RacLogin
//
//  Created by HB on 2017/10/16.
//  Copyright © 2017年 com.comzenz-service. All rights reserved.
//

#import "ViewController.h"
#import "BaseUse.h"


@interface ViewController ()

@property (nonatomic, strong) id<RACSubscriber> subscrib;

@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self normalSignal];
    
    [self subjectSignal];
    
    [self playSubjectSignal];
    
    [self racArray];
    
    [self tuple];
    
    [self dict];
    
    [self dicTransModel];

    [self raclifselector];
    
    [self HongRac];
    
    [self multiconnectSignal];
    
    [self RacComman];
    
    // Do any additional setup after loading the view, typically from a nib.
}

// 普通信号
- (void)normalSignal {
    RACSignal *sg = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
//        _subscrib = subscriber  // 强引用
        [subscriber sendNext:@"普通信号"];
        return [RACDisposable disposableWithBlock:^{
            
            NSLog(@"订阅被取消");
        }];
    }];
    
    // 订阅信号
    RACDisposable *disposeable = [sg subscribeNext:^(id  _Nullable x) {
        NSLog(@"信号传过来了%@",x);
    }];
    [disposeable dispose]; // 订阅完成后，默认自动取消订阅、 强引用之后必须dispose才能取消订阅。
    
    
}

// subject信号
- (void)subjectSignal {
    RACSubject *sbj = [RACSubject subject];
    [sbj subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    [sbj sendNext:@"subject信号"];
}

- (void)playSubjectSignal {
    RACReplaySubject *reSb = [RACReplaySubject subject];
    [reSb sendNext:@"resb"];
    [reSb subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
}

- (void)racArray {
    NSArray *arr = @[@"213",@"321",@1];
    [arr.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
}

- (void)tuple {
    RACTuple *tuple = [RACTuple tupleWithObjectsFromArray:@[@"213",@"321",@1]];
    NSString *str = [tuple objectAtIndex:1];
    NSLog(@"%@",str);
    [tuple.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
}

- (void)dict {
    NSDictionary *dict = @{@"name":@"aaa",@"age":@18,@"account":@"sub"};
    [dict.rac_sequence.signal subscribeNext:^(RACTuple  *x) {
        RACTupleUnpack(NSString *key, NSString *value) = x;
        NSLog(@"%@,%@",key,value);
    }];
}

- (void)dicTransModel {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
    NSArray *dicArr = [NSArray arrayWithContentsOfFile:filePath];
    
//     NSMutableArray *arr = [NSMutableArray array];
//    [dicArr.rac_sequence.signal subscribeNext:^(NSDictionary *x) {
////        Flag *flag = [Flag flagWithDict:x];
//        //        [arr addObject:flag];
//    }];
    
//    NSArray *arr = [[dicArr.rac_sequence map:^id(NSDictionary *value) {
//
//        return [Flag flagWithDict:value];
//    }] array];
    
    
}

// 两个网络请求完成后才更新UI
- (void)raclifselector {
    RACSignal *s1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"s1请求到数据");
        // 请求到数据
        sleep(2);
        [subscriber sendNext:@"134"];
        
        return nil;
    }];
    RACSignal *s2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"s2请求到数据");
        // 请求到数据
        sleep(2);
        [subscriber sendNext:@"246"];
        return  nil;
    }];
    [self rac_liftSelector:@selector(updateUI:andA2:) withSignalsFromArray:@[s1,s2]];
}


- (void)updateUI:(NSString *)a1 andA2:(NSString *)a2 {
    NSLog(@"a1 = %@, \n a2 = %@",a1, a2);
}

- (void)HongRac {
//    [_textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
//        _textLabel.text = x;
//    }];
    
    // 等同于
    RAC(_textLabel,text) = _textField.rac_textSignal;
    
    
//    RACObserve(self.view, frame);  监听对象的属性一改变就会产生信号 相当于
//    [self.view rac_valuesForKeyPath:<#(NSString *)#> observer:<#(NSObject *__weak)#>]
//    [RACObserve(self.view, frame) subscribeNext:^(id  _Nullable x) {
//
//    }]; // 监听frame改变的时候修改什么
    
}

// 多次订阅信号的时候，防止信号block中请求、处理多次
- (void)multiconnectSignal {
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        NSLog(@"发送请求");
        [subscriber sendNext:@"123"];
        
        return  nil;
    }];
    
    RACMulticastConnection *connect = [signal publish];
    
    [connect.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"第一次订阅%@",x);
    }];
    
    [connect.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"第二次订阅%@",x);
    }];
    
    [connect connect];
}

// rac 中用于处理事件
- (void)RacComman {
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        
        NSLog(@"input = %@", input);
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSLog(@"请求数据");
            [subscriber sendNext:@"热门区的数据"];
            [subscriber sendCompleted];
            return nil;
        }];
    }];
    
    [command.executing subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue] == YES) { // 当前正在执行
            NSLog(@"正在执行");
        } else {
            // 执行完成或者没有执行
            NSLog(@"没有执行/ 执行完成");
        }
    }];
    
    [[command execute:@"hotZone"] subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"订阅了热区的数据，%@",x);
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
